package com.nti56.springboot.controller;



import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.nti56.springboot.model.Organization;
import com.nti56.springboot.model.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nti56.springboot.cache.BaseDataCache;
import com.nti56.springboot.model.AjaxResult;
import com.nti56.springboot.service.OrganizationService;
import com.nti56.springboot.utils.Constant;
import com.nti56.springboot.utils.StringUtil;



@RequestMapping("org")
@Controller
public class OrganizationController extends BaseController{
    
	private static final Logger logger = LoggerFactory.getLogger(OrganizationController.class);

	
	@Autowired
    private OrganizationService organizationService;
	
	@Autowired
    private BaseDataCache baseDataCache;
	
	
	@RequestMapping("/toOrgList")
    public String toOrgList(Model model){
		model.addAttribute("orgRootId", Constant.HBZY_ORGANIZATION_ROOT_ID);
        return "system/orgList";
    }
	
	@RequestMapping("/getOrgById")
    @ResponseBody
    public Organization getOrgById(String orgId){
		List<Organization> list = baseDataCache.getOrganizations();
		for(Organization org : list) {
			if(orgId.equals(org.getOrgId())) {
				return org;
			}
		}
		return null;
    }
	

	@RequestMapping("toOrgAdd")
	public String toOrgAdd(HttpServletRequest request, Model model){
		String orgId = (String) request.getParameter("orgId");
		String pId = (String) request.getParameter("pId");
		if(!StringUtil.isEmpty(orgId)) {
			Organization org = organizationService.getOrganizationById(orgId);
			model.addAttribute("org", org);
		}
		model.addAttribute("orgId", orgId);
		model.addAttribute("pId", pId);
        return "system/orgAdd";
	}
	
	@RequestMapping("/addOrg")
    @ResponseBody
    public AjaxResult addOrg(HttpServletRequest request,Organization org){    	
    	try {    		
    		String orgId = (String) request.getParameter("orgId");
			if(StringUtil.isEmpty(orgId)) {
				organizationService.insert(org);
				return new AjaxResult(true,AjaxResult.SAVE_OK);
			}else {
				organizationService.update(org);
				return new AjaxResult(true,AjaxResult.UPDATE_OK);
			}			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.SAVE_FAIL);
		}
    }
	
	
	@RequestMapping("/getAllOrganization")
    @ResponseBody
    public List<TreeNode> getAllOrganization(){
		List<TreeNode> list = baseDataCache.getTreeNode4Org();
		return list;
    }
	
	
	@RequestMapping("/getAllDCOrgList")
    @ResponseBody
    public List<TreeNode> getAllDCOrgList(){
		List<TreeNode> list = baseDataCache.getTreeNode4DCOrg();
		return list;
    }
	
	/**
	 * 逻辑删除
	 * @param orgId
	 * @return
	 */
	@RequestMapping("/deleteOrg")
    @ResponseBody
    public AjaxResult deleteOrg(String orgId){
    	try {
    		organizationService.delete(orgId);
			return new AjaxResult(true,AjaxResult.DLELETE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.DLELETE_FAIL);
		}
    }

	
}